package org.ziegler.log.analysis.resultFilter;

import java.time.LocalDateTime;
import java.util.Objects;

import org.ziegler.log.analysis.core.SplitColumn;

/**
 * 时间段过滤
 *
 * @author Ziegler
 * date 2020/7/20 21:17
 */
public class ResultDateFilter implements ResultFilter {

    private final LocalDateTime beginDateTime;

    private final LocalDateTime endDateTime;

    public ResultDateFilter(LocalDateTime beginDateTime, LocalDateTime endDateTime) {
        if (beginDateTime.isAfter(endDateTime)) {
            final String s = "beginDateTime比endDateTime时间要晚。beginDateTime = " + beginDateTime
                    .toString() + ", endDateTime = " + endDateTime.toString();
            throw new IllegalArgumentException(s);
        }

        this.beginDateTime = Objects.requireNonNull(beginDateTime);
        this.endDateTime = Objects.requireNonNull(endDateTime);
    }

    public ResultDateFilter(LocalDateTime beginDateTime) {
        this.beginDateTime = Objects.requireNonNull(beginDateTime);
        this.endDateTime = LocalDateTime.MAX;
    }

    @Override
    public boolean test(SplitColumn splitColumn) {
        if (beginDateTime.isBefore(splitColumn.getDateTime())
                && endDateTime.isAfter(splitColumn.getDateTime())) {
            // 只获取在开始和结束时间范围内的数据
            return true;
        }

        return false;
    }
}
